<template>
  <div class="detail">
    <header>
      <div class="header-returns" v-if="isShow" @click="goBack">
        <i class="iconfont icon-weibiaoti--"></i>
        <i class="iconfont icon-home"></i>
      </div>
      <div class="header-bar" :style="styleOptions" v-else>
        <i class="iconfont icon-weibiaoti--"></i>
        <div>
          <span>商品详情</span>
          <span>商品评价</span>
        </div>
        <i class="iconfont icon-kefu"></i>
      </div>
    </header>
    <section class="detail-main">
      <div>
        <div class="swiper-main">
          <swiper :options="swiperOption">
            <swiper-slide v-for="(item, index) in swiperList" :key="index"
              ><img :src="item.imgUrl" alt="" />
            </swiper-slide>
          </swiper>
          <div class="swiper-pagination"></div>
        </div>
        <div class="goods-name">
          <h1>{{ goods.name }}</h1>
          <div>性价首选，茶感十足、鲜醇耐泡的大众口粮茶</div>
        </div>
        <div class="goods-price">
          <div class="pprice">
            <span>￥</span>
            <b>{{ goods.price }}</b>
          </div>
          <div class="oprice">
            <span>价格：</span>
            <del>￥288</del>
          </div>
        </div>
        <div v-for="(item, index) in swiperList" :key="index">
          <img :src="item.imgUrl" alt="" />
        </div>
      </div>
    </section>
    <footer>
      <div class="add-cart" @click="addCart">加入购物车</div>
      <div>立即购买</div>
    </footer>
  </div>
</template>

<script>
import "swiper/dist/css/swiper.css";
import { swiper, swiperSlide } from "vue-awesome-swiper";
//引入滑动插件
import BetterScroll from "better-scroll";
//引入封装后的axiox
import http from "@/common/api/request";
import { Toast } from "vant";
import store from "@/store";
export default {
  name: "Detail",
  components: { swiper, swiperSlide },
  data() {
    return {
      goods: {},
      styleOptions: {},
      BetterScroll: "",
      isShow: true,
      swiperOption: {
        autoplay: 3000,
        speed: 1000,
        pagination: {
          el: ".swiper-pagination",
          type: "fraction",
        },
      },
      swiperList: [
        {
          imgUrl: "/images/goods-list1.jpeg",
        },
        {
          imgUrl: "/images/goods-list1.jpeg",
        },
        {
          imgUrl: "/images/goods-list1.jpeg",
        },
      ],
    };
  },
  methods: {
    addCart() {
      if (!store.state.user.token) {
        Toast("请先登录您的账号！");
        return;
      }
      let id = this.$route.query.id;
      http
        .$axios({
          url: "/api/addCart",
          method: "Post",
          data: {
            goodsId: id,
          },
          headers: {
            token: "true",
          },
        })
        .then((res) => {
          if (res.success) {
            Toast(res.msg);
          }
        });
    },
    goBack() {
      this.$router.back();
    },
    async getData() {
      let id = this.$route.query.id;
      let res = await http.$axios({
        url: "/api/goods/id",
        params: { id },
      });
      this.goods = res;
      for (let index in this.swiperList) {
        this.swiperList[index].imgUrl = res.imgUrl;
      }
    },
  },

  created() {
    this.id = this.$route.query.id;
    this.getData();
    // console.log(this.$route.query.id);
    //console.log(this.$route.params.id);
  },
  mounted() {
    //当dom都加载完毕了再执行
    this.$nextTick(() => {
      this.BetterScroll = new BetterScroll(".detail-main", {
        movable: true,
        zoom: true,
        click: true,
        probeType: 3, //开启scroll事件
        bounce: false, //取消滚到顶部或底部的弹性效果
      });
      this.BetterScroll.on("scroll", (pos) => {
        let posY = Math.abs(pos.y); //abs取绝对值
        let opacity = posY / 180;
        opacity = opacity > 1 ? 1 : opacity;
        this.styleOptions = {
          opacity: opacity,
        };
        if (posY >= 50) {
          this.isShow = false;
        } else {
          this.isShow = true;
        }
      });
    });
  },
  activated() {
    if (this.$route.query.id != this.id) {
      this.getData();
      this.id = this.$route.query.id;
    }
  },
};
</script>

<style scoped lang="scss">
.detail-main {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
header {
  position: fixed;
  width: 100%;
  height: 1.173333rem;
  left: 0;
  top: 0;
  z-index: 999;
  .header-returns {
    display: flex;
    width: 100%;
    height: 1.173333rem;
    justify-content: space-between;
    align-items: center;
    i {
      width: 1rem;
      line-height: 1rem;
      margin: 0 0.266666rem;
      font-size: 0.7rem;
      color: #fff;
      background-color: rgba(0, 0, 0, 0.3);
      border-radius: 100%;
      text-align: center;
    }
  }
}
.header-bar {
  display: flex;
  width: 100%;
  height: 1.173333rem;
  justify-content: space-between;
  align-items: center;
  font-size: 0.7rem;
  background-color: #fff;
  i {
    width: 1rem;
    line-height: 1rem;
    margin: 0 0.266666rem;
    font-size: 0.7rem;
    color: #9999;
  }
  span {
    padding: 0 0.266666rem;
    font-size: 0.426666rem;
    line-height: 1.173333rem;
  }
}

section {
  img {
    width: 100%;
    height: 500px;
  }
}
footer {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1.306666rem;
  box-shadow: 0 1px 10px 1px rgba($color: #ccc, $alpha: 0.8);
  background-color: #fff;
  div {
    width: 50%;
    line-height: 1.306666rem;
    font-size: 0.426666rem;
    text-align: center;
    color: #fff;
    background-color: red;
  }
  .add-cart {
    background-color: #ff9500;
  }
}
.goods-name {
  padding: 0.533333rem 0.266666rem;
  border-bottom: 1px solid #ccc;

  h1 {
    font-size: 0.533333rem;
    font-weight: 500;
  }
  div {
    font-size: 0.373333rem;
    color: #9999;
  }
}

.goods-price {
  padding: 0.533333rem 0.266666rem;
  .pprice {
    color: red;
  }
  .oprice {
    color: #9999;
    font-size: 0.32rem;
  }
}
.swiper-main {
  position: relative;
  width: 100%;
  height: 10rem;
}
.swiper-container {
  width: 100%;
  height: 10rem;
}

.swiper-main img {
  width: 100%;
  height: 10rem;
}

.swiper-pagination {
  box-sizing: border-box;
  padding-right: 0.4rem;
  bottom: 0.2rem;
  color: #fff;
  text-align: right;
  font-size: 0.426666rem;
}

.swiper-pagination-bullet-active {
  background-color: #b0352f;
}
.swiper-pagination-bullet {
  padding: 0.08rem;
  margin: 0.2rem;
}
</style>